package com.xx.jfb.server;

import java.util.HashMap;
import java.util.Map;

public class XXRequest {
    private String type;
    private String url;

    private Map<String,String> params;

    private String content;

    public XXRequest build(String content) throws Exception {
        String reqType =  content.substring(0, 5);
        if(reqType.contains("GET")){
            String [] array = content.split("\\n");
            String [] gens = array[0].split(" ");
            this.type = gens[0];
            this.url=gens[1].split("\\?")[0];
            try {
                this.params = getGetParams(gens[1].split("\\?")[1]);
            }catch (Exception e){
                System.out.println("build error:" + e.getMessage());
            }
        }else if(reqType.contains("POST")){
            this.type = "POST";
            System.err.println("不支持POST请求");
            throw new Exception("不支持POST请求");
        }
        return this;
    }

    private Map<String,String> getGetParams(String params){
        String [] array = params.split("&");
        Map<String,String> map = new HashMap<>();
        for(String param : array){
            String [] strs = param.split("=");
            map.put(strs[0],strs[1]);
        }
        return map;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public String getType() {
        return type;
    }

    public String getUrl() {
        return url;
    }
}
